<?php


# filesystem interface provides list directory, make a directory,
# and add file, delete file, move file

class FileSystemIO
{
	public function __construct()
	{
	}

	# return: an array of sub files
	# array format: (filename, filepath, isdir)
	//    public function ls($strDir)
	//    {
	//
	//        $handle = opendir($strDir);
	//        $fileList = array();
	//        readdir($handle);
	//        readdir($handle);
	//
	//        while ($filename = readdir($handle))
	//        {
	//            $file = array();
	//            $file['filename']  = $filename;
	//            $file['filepath']  = "$strDir/$filename";
	//            $file['isdir']     = 0;
	//            $file['size']  = filesize($filename);
	//            $file['modification']  = date("Y-m-d", filemtime($filename));
	//
	//            if (is_dir($file['filepath'])) //鎵�緱鍒扮殑鏂囦欢鍚嶆槸鍚︽槸涓�釜鐩綍
	//            {
	//                $file['isdir'] = 1;
	//            }
	//            $fileList[] = $file;
	//        }
	//
	//        return $fileList;
	//    }

	public function ls($strDir)
	{
		$dir = new DirectoryIterator($strDir);
		$tail=array("B","K","M","G");
		foreach ($dir as $fileinfo)
		{
			$file = array();
			$file['filename']  = $fileinfo->getFilename();
			$file['filepath']  = $fileinfo->getPathname();
			$file['isdir']     = 0;
			
			//获得文件的大小，并且带上单位
			$size = $fileinfo->getSize();
			$i=0;
			while($i<3){
				if($size>1024){
					$size=$size/1024;
				}else{
					break;
				}
				$i++;
			}
			
			$file['size'] = sprintf("%.2f%s",$size,$tail[$i]);
			$file['modification']  = Date("Y-m-d",$fileinfo->getCTime());

			if ($fileinfo->isDir())
			{
				$file['isdir'] = 1;
			}
			$fileList[] = $file;
		}

		return $fileList;
	}



	public function mkdir($strDir)
	{
		mkdir($strDir);
	}

	public function add($strFilePath)
	{

	}

	public function rmdir($strDir)
	{
		$fileList = $this->ls($strDir);
		foreach ($fileList as $file)
		{
			if ($file['isdir'] == 1)
			{
				$this->rmdir($file['filename']);
			}
			else
			{
				$this->rm($file['filename']);
			}
		}
	}

	public function rm($strFile)
	{
		if (file_exists($strFile))
		{
			unlink($strFile);
		}
	}

	# copy source file to destination file
	public function copy($srcFile, $destFile)
	{
		if (file_exists($srcFile))
		{
			copy($srcFile, $destFile);
		}
	}

	# move source file to destination file
	public function move($srcFile, $destFile)
	{
		if (file_exists($srcFile))
		{
			rename($srcFile, $destFile);
		}
	}

};

